﻿using AJWPFAdmin.Core.Components.Views;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;

namespace AJWPFAdmin.Core.Components.AJTable.ViewModels
{
    public enum AJTableColumnFixedType
    {
        Left,
        Right
    }
    public class AJTableColumnItem : BindableBase
    {
        public string DataIndex { get; set; }
        public string ExtraDataIndex { get; set; }
        public string Title { get; set; }
        public AJTableCustomHeader CustomerHeader { get; set; }
        /// <summary>
        /// 列宽度，如果是固定在右侧，则建议设置此值
        /// </summary>
        public DataGridLength Width { get; set; } = DataGridLength.Auto;
        public double? MinWidth { get; set; }
        public double? MaxWidth { get; set; }

        private AJTableColumnFixedType? _fixed;
        public AJTableColumnFixedType? Fixed
        {
            get { return _fixed; }
            set { SetProperty(ref _fixed, value); }
        }

        private bool _visible = true;
        public bool Visible
        {
            get { return _visible; }
            set { SetProperty(ref _visible, value); }
        }

        private bool _isSummaryField;
        /// <summary>
        /// 是否是统计字段
        /// </summary>
        public bool IsSummaryField
        {
            get { return _isSummaryField; }
            set { SetProperty(ref _isSummaryField, value); }
        }

        private bool _isExport = true;
        /// <summary>
        /// 是否是导出字段，默认true， 一般配合 FetchOption.ExportWithCurrentColumns 
        /// </summary>
        public bool IsExport
        {
            get { return _isExport; }
            set { SetProperty(ref _isExport, value); }
        }

        public AJTableColumnFormatter Formatter { get; set; }

        public AJTableCustomTemplate CustomTemplate { get; set; }

        internal DataGridColumn CreateColumn()
        {
            DataGridColumn col;
            col = CustomTemplate != null ? CreateTemplateColumn() : CreateTextColumn();

            col.Header = Title;
            col.Width = Width;
            col.Visibility = Visible ? Visibility.Visible : Visibility.Collapsed;

            if (MinWidth.HasValue)
            {
                col.MinWidth = MinWidth.Value;
            }

            if (MaxWidth.HasValue)
            {
                col.MaxWidth = MaxWidth.Value;
            }

            if (CustomerHeader != null)
            {
                var baseStyle = Application.Current.Resources["MaterialDesignDataGridColumnHeader"] as Style;
                var s = new Style(typeof(DataGridColumnHeader), baseStyle);
                s.Setters.Add(new Setter(FrameworkElement.HorizontalAlignmentProperty, CustomerHeader.HorizontalAlignment));
                col.HeaderStyle = s;
            }
            return col;
        }

        private DataGridColumn CreateTemplateColumn()
        {
            return new AJDataGridTemplateColumn
            {
                ColumnKey = DataIndex ?? Guid.NewGuid().ToString("N"),
                Config = this
            };
        }

        private DataGridColumn CreateTextColumn()
        {
            return new AJDataGridTextColumn
            {
                ColumnKey = DataIndex ?? Guid.NewGuid().ToString("N"),
                Config = this
            };
        }

        public AJTableColumnItem()
        {

        }

    }
}
